package com.qf.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Component
public class MailTools {

    @Autowired
    private JavaMailSender javaMailSender;
    //发送者
    @Value("${spring.mail.username}")
    private String from;
    //接收者

    /**
     *
     * @param to 接收验证的邮箱地址
     * @throws MessagingException
     */
    public void send(String to,String checkcode) throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);//带格式
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);
        mimeMessageHelper.setSubject("注册验证码");
        mimeMessageHelper.setText("<font color='blue'>"+checkcode+"</font>",true);
        javaMailSender.send(mimeMessage);
    }
}
